import java.util.ArrayList;
import java.util.List;

public class Demo {
}
class Solution {
    List<Integer>[] list;
    boolean[] vis;
    public long countPairs(int n, int[][] edges) {
        long ans = 0, s = 0;
        list = new ArrayList[n];
        vis = new boolean[n];
        for(int i=0; i<n; i++)
            list[i] = new ArrayList<>();
        for(int[] x : edges){
            list[x[0]].add(x[1]);
            list[x[1]].add(x[0]);
        }
        for(int i=0; i<n; i++){
            int t = dfs(i);
            ans += s*t;
            s += t;
        }
        return ans;
    }
    int dfs(int i){
        if(vis[i]) return 0;
        vis[i] = true;
        int cnt = 1;
        for(int j : list[i])
            cnt += dfs(j);
        return cnt;
    }
}